/*
 * Copyright (C) 2019 Honeywell, Inc. All Rights Reserved.
 */
package org.thanos.iot.dao.graphic;

import com.honeywell.tj.iot.entity.graphic.GraphicView;
import com.honeywell.tj.iot.vo.graphic.ViewVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * @ClassName ViewMapper
 * @Author H241118
 * @Date 3/9/2020 4:20 PM
 **/
@Mapper
public interface GraphicViewMapper {

    @Results({
            @Result(property = "id", column = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(property = "name", column = "name", jdbcType = JdbcType.VARCHAR),
            @Result(property = "width", column = "width", jdbcType = JdbcType.INTEGER),
            @Result(property = "height", column = "height", jdbcType = JdbcType.INTEGER),
            @Result(property = "locationId", column = "location_id", jdbcType = JdbcType.INTEGER),
            @Result(property = "active", column = "active", jdbcType = JdbcType.BIT)
    })
    @Select("SELECT id, name, width, height, location_id, active FROM iot_graphic_view WHERE location_id = #{locationId} ORDER BY name")
    List<GraphicView> selectViewsByLocationId(Integer locationId);

    @Results({
            @Result(property = "id", column = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(property = "name", column = "name", jdbcType = JdbcType.VARCHAR),
            @Result(property = "width", column = "width", jdbcType = JdbcType.INTEGER),
            @Result(property = "height", column = "height", jdbcType = JdbcType.INTEGER),
            @Result(property = "locationId", column = "location_id", jdbcType = JdbcType.INTEGER),
            @Result(property = "active", column = "active", jdbcType = JdbcType.BIT)
    })
    @Select("SELECT id, name, width, height, location_id, active FROM iot_graphic_view WHERE id = #{id} LIMIT 1")
    GraphicView selectViewById(Integer id);

    @Results({
            @Result(property = "id", column = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(property = "name", column = "name", jdbcType = JdbcType.VARCHAR),
            @Result(property = "width", column = "width", jdbcType = JdbcType.INTEGER),
            @Result(property = "height", column = "height", jdbcType = JdbcType.INTEGER),
            @Result(property = "locationId", column = "location_id", jdbcType = JdbcType.INTEGER),
            @Result(property = "active", column = "active", jdbcType = JdbcType.BIT)
    })
    @Select("SELECT id, name, width, height, location_id, active FROM iot_graphic_view WHERE name = #{name} AND location_id = #{locationId} LIMIT 1")
    GraphicView selectViewByNameAndLocationId(@Param("name") String name, @Param("locationId") Integer locationId);

    @Select("SELECT id, name FROM iot_graphic_view WHERE name = #{name} LIMIT 1")
    GraphicView selectViewByName(String name);

    @Select("SELECT name FROM iot_graphic_view WHERE location_id = #{locationId} AND active = TRUE LIMIT 1")
    String selectActiveViewByLocationId(Integer locationId);


    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "items", column = "id", many = @Many(select = "com.honeywell.tj.iot.mapper.graphic.GraphicItemMapper.selectItemsByViewId"))
    })
    @Select("SELECT id, name, width, height, location_id, active FROM iot_graphic_view WHERE id = #{id} LIMIT 1")
    GraphicView selectViewAndItemsByViewId(Integer id);

    @Select("SELECT id FROM iot_graphic_view WHERE location_id = #{locationId} AND active = TRUE LIMIT 1")
    Integer selectActiveViewIdByLocationId(Integer locationId);

    @Select("SELECT id FROM iot_graphic_view")
    List<Integer> selectViews();

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("INSERT INTO iot_graphic_view(name, width, height, location_id, active) VALUES (#{name}, #{width}, #{height}, #{locationId}, #{active})")
    Integer insertView(ViewVo view);

    @Update("UPDATE iot_graphic_view SET active = FALSE WHERE location_id = #{locationId} AND active = TRUE")
    Integer updateActiveViewByLocationId(Integer locationId);

    @Update("UPDATE iot_graphic_view SET name = #{name}, width = #{width}, height = #{height}, location_id = #{locationId}, active = #{active} WHERE id = #{id}")
    Integer updateViewById(@Param("name") String name, @Param("id") Integer id, @Param("width") Integer width, @Param("height") Integer height, @Param("locationId") Integer locationId, @Param("active") Boolean active);

    @Delete("DELETE FROM iot_graphic_view WHERE id = #{id}")
    Integer deleteView(GraphicView graphicView);
}
